package com.jiaai.cloud.wx.schedular;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.wx.entity.TPay;
import com.jiaai.cloud.wx.service.ITPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Component
@EnableScheduling
public class PayScheduledTasks{

    public static ArrayList<TPay> PAY_LIST = new ArrayList<>();
    @Autowired
    private ITPayService itPayService;

    private static final Logger LOGGER = LoggerFactory.getLogger(PayScheduledTasks.class);

    static {
        Collections.synchronizedList(PAY_LIST);
    }

    /**
     * 当软件重启后向数据库查询处于可转账的工资信息 ===============单线程
     */
    @Scheduled(fixedRate = 1000 * 60 * 60 * 24)
    public void queryPayInStart() throws InterruptedException {
        LOGGER.info("转账队列");
        PAY_LIST = (ArrayList<TPay>) itPayService.selectList(new EntityWrapper<TPay>()
                .eq("status", 1));
    }

    /**
     * 转账队列, 三小时执行一次, 串行对接微信支付api, setStatus(2)=================多线程
     * @throws MessagingException
     * @throws IOException
     * @throws InterruptedException
     */
    public void work() throws MessagingException, IOException, InterruptedException {

        for (TPay e : PAY_LIST){
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + PAY_LIST.size() + "\t\t" + "1234567894561354678945613");
        }
    }



}
